package {
	import com.baseoneonline.flash.utils.FPSMeter;
	import com.baseoneonline.flash.utils.KeyCode;
	import com.baseoneonline.flash.utils.KeyHandler;
	import com.baseoneonline.flash.utils.LogView;
	import com.baseoneonline.flash.utils.logger.Logger;
	import com.borderbears.frontend.Application;
	import com.borderbears.frontend.SceneView;
	import com.borderbears.frontend.events.BBEvent;
	import com.borderbears.frontend.util.Cursor;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.utils.getQualifiedClassName;

	public class BorderBears3D extends Sprite
	{
		
		private var log:Logger = Logger.getInstance(getQualifiedClassName(this));
		private var app:Application;
		private var logView:LogView;
		private var fpsView:FPSMeter;
		private var sceneView:SceneView;
		private var key:KeyHandler;
		
		public function BorderBears3D()
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			
			
			app = Application.getApplication();	
			
			

			app.addEventListener(BBEvent.TOGGLE_DEBUG, onToggleDebug);

			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

			fpsView = new FPSMeter();
			fpsView.visible = false;
			
			logView = new LogView();
			logView.visible = false;
			
			log.info("Loading config...");

			//Config.getConfig().load("config.xml", init);
			// VIEWS
			init();
		}
		
		private function onToggleDebug(e:BBEvent=null):void {
			var debug:Boolean = app.getIsDebug();
			logView.visible = debug;
			fpsView.visible = debug;
		}
		
		private function init():void {
			

			
			key = new KeyHandler(stage);

			sceneView = new SceneView();
			
			addChild(sceneView);
			addChild(logView);
			addChild(fpsView);
			
			
			onToggleDebug();
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
			addChild(Cursor.getCursor().setup());
		}
		
		private function onEnterFrame(e:Event):void {
			
			sceneView.update(1);
		}
		
		private function onKeyDown(e:KeyboardEvent):void {
			if (e.keyCode == KeyCode.KEY_TILDE) {
				app.setIsDebug(!app.getIsDebug());
			}
			if (e.keyCode == KeyCode.KEY_LBRACKET) {
				sceneView.nextCamera();
			}
		
		} 
	}
}
